import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';
import 'package:get/get.dart';

import '../../../../bean/bean_score_integral.dart';
import '../controller/scroe_integral_recoder_controller.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/13.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///
class MyScoreIntegralRecoderPage extends StatefulWidget {
  final int userId;
  final String startDate;

  const MyScoreIntegralRecoderPage(
      {Key key, @required this.userId, @required this.startDate})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return MyScoreIntegralRecoderPageState();
  }
}

class MyScoreIntegralRecoderPageState
    extends State<MyScoreIntegralRecoderPage> {
  @override
  void initState() {
    super.initState();
    LogUtils.e("查看积分记录 ${widget.userId}");
    Get.put(ScroeIntegralRecoderController(widget.userId, widget.startDate));
    Future.delayed(Duration.zero, () {
      Get.find<ScroeIntegralRecoderController>()
          .requestWaterList(id: widget.userId);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("积分获取记录"),
      ),

      ///填充布局
      body: RefreshIndicator(
        onRefresh: () {
          return Get.find<ScroeIntegralRecoderController>().onRefresh();
        },
        child: Container(
          width: double.infinity,
          height: double.infinity,
          child: GetBuilder(
            id: "IntegralRecoderWater",
            builder: (ScroeIntegralRecoderController controller) {
              List<ScoreIntegralWaterBean> waterList = controller.waterList;
              if (controller.isLoading) {
                return Center(child: LoadingWidget());
              }
              if (waterList.length == 0) {
                return Center(
                  child: NoDataWidget(
                    clickCallBack: () {
                      Get.find<ScroeIntegralRecoderController>()
                          .requestWaterList(isRefresh: true);
                    },
                  ),
                );
              }
              return ListView.builder(
                itemCount: waterList.length,
                itemBuilder: (BuildContext context, int index) {
                  ScoreIntegralWaterBean item = waterList[index];
                  bool isShowTitle = true;
                  bool isShowBottomTitle = true;
                  Color textColor = Colors.red;
                  String preText = "+";
                  if (index > 0) {
                    ScoreIntegralWaterBean preItem = waterList[index - 1];
                    if (item.createTimeStr == preItem.createTimeStr) {
                      isShowTitle = false;
                    }
                  }
                  if (index < waterList.length - 1) {
                    ScoreIntegralWaterBean netxItem = waterList[index + 1];
                    if (item.createTimeStr == netxItem.createTimeStr) {
                      isShowBottomTitle = false;
                    }
                  }
                  return Container(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        isShowTitle
                            ? Padding(
                          padding: EdgeInsets.only(bottom: 12, top: 12),
                          child: Row(
                            children: [
                              SizedBox(width: 8),
                              Container(
                                child: Text(
                                  item.createTimeStr,
                                  style: TextStyle(
                                      fontWeight: FontWeight.w600,
                                      color: Colors.black),
                                ),
                              ),
                            ],
                          ),
                        )
                            : SizedBox(),
                        Container(
                            color: Colors.white,
                            padding:
                            EdgeInsets.only(left: 12, top: 12, right: 12),
                            child: Column(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Expanded(
                                      child: Column(
                                        crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                        mainAxisSize: MainAxisSize.min,
                                        children: [
                                          Text(
                                            item.title,
                                            style: TextStyle(
                                                fontSize: 14,
                                                color: Colors.black),
                                          ),
                                          SizedBox(
                                            height: 8,
                                          ),
                                          Text(
                                            item.times,
                                            style: TextStyle(
                                                fontSize: 12,
                                                color: Color(0xff666666)),
                                          )
                                        ],
                                      ),
                                    ),
                                    Text(
                                      "$preText ${item.integralNum}",
                                      style: TextStyle(
                                          color: textColor,
                                          fontSize: 16,
                                          fontWeight: FontWeight.w600),
                                    )
                                  ],
                                ),
                                Container(
                                  width: double.infinity,
                                  height: 1,
                                  color: Color(0xffeeeeee),
                                  margin: EdgeInsets.only(top: 10),
                                )
                              ],
                            ))
                      ],
                    ),
                  );
                },
              );
            },
          ),
        ),
      ),
    );
  }
}
